#!/usr/bin/perl
# 
#  Exercise 7.5 
# 
# Write a subroutine that, given a codon (a fragment of DNA of length 3), returns a 
# random mutation in the codon.
#
# Answer to Exercise 7.5

use strict;
use warnings;

my $codon = 'AAC';

my $mutated_codon = mutate_codon($codon);

print "$codon mutates to $mutated_codon\n";

exit;

# Note that we could have used the subroutines defined in
# Example 7-2 such as 'mutate', 'randomposition', and 'randomnucleotide'.
# But here we decided to just write a single subroutine for the purpose,
# since we were able to write a very short one.

sub mutate_codon {

	my($codon) = @_;

	my @bases = qw(A C G T);

	my $position = int rand 3;

	my $base = $bases[$position];

	my $newbase;

	do {
		$newbase = $bases[rand @bases];
	} until ($newbase ne $base);

	substr($codon, $position, 1) = $newbase;

	return $codon;
}

